<?php
/**
 * 店铺后台统计
 * @author Max.Yu <max@jipu.com>
 */

namespace Shop\Controller;

use Think\Page;

class StatController extends CommonController
{

    /**
     * 订单统计
     */
    public function index()
    {
        $today = date('Y-m-d');
        $start_time = I('get.start_time', '');
        $end_time = I('get.end_time', '');
        $reload = I('get.reload', '');
        if ($reload) {
            unset($_GET['reload'], $_GET['formhash']);
        }

        $cacheName = 'stat_order_' . md5(serialize(I('get.') . $this->store_id));
        if (!S($cacheName) || $reload == 1) {
            S($cacheName, null);
            //统计内容
            $stat = array(
                'shopname' => get_store_text($this->store_id),
            );
            //统计信息
            $stat['stat_num'] = D('Stat')->getOrderStatNum($this->store_id, $start_time, $end_time);
            //获取图表数据
            $stat['chartData'] = D('Stat')->getOrderChartData($this->store_id, $start_time, $end_time);
            if (isset($stat['chartData']['error'])) {
                $this->error($stat['chartData']['error']);
            }
            $stat['cacheTime'] = time();
            S($cacheName, $stat, 3600);
            if (IS_AJAX) {
                $this->success(I('get.'));
            }
        } else {
            $stat = S($cacheName);
            if ((time() - $stat['cacheTime']) < 20) {
                $stat['is_new'] = 1;
            }
        }
        $this->today = $today;
        $this->start_time = $start_time;
        $this->end_time = $end_time;
        $this->stat = $stat;
        $this->meta_title = '订单统计';
        $this->display();
    }


    public function goods()
    {

        //默认取当月
        $start_time = I('get.start_time');
        $end_time = I('get.end_time');
        $sort = I('get.end_time', 'desc');
        !$start_time && $start_time = date("Y-m-" . '01');
        $today = date('Y-m-d');
        //统计信息
        $stat['stat_num'] = D('Stat')->getItemStat($this->store_id, $start_time, $end_time);
        //排序
        if ('desc' == $sort) {
            arsort($stat['stat_num'], SORT_NUMERIC);
        } else {
            asort($stat['stat_num'], SORT_NUMERIC);
        }
        //商品信息
        $where['id'] = array('in', array_keys($stat['stat_num']));
        $where['store_id'] = array('eq', $this->store_id);
        if ($stat['stat_num']) {
            $order = "field(id, " . implode(',', array_keys($stat['stat_num'])) . ")";
            $stat['lists'] = M('Item')->field('id, number, name, category')->where($where)->order($order)->cache(true, 86400)->select();
        }
        //合并数据
        foreach ($stat['lists'] as $k => $v) {
            $stat['lists'][$k]['buynum'] = $stat['stat_num'][$v['id']];
            //防止导出科学计数法编号
            $stat['lists'][$k]['number'] = ' ' . $stat['lists'][$k]['number'];
        }
        $item_id = I('item_id', 0, intval);
        $this->today = $today;
        $this->start_time = $start_time;
        $this->end_time = $end_time;
        $this->stat = $stat;
        $this->meta_title = '商品销量';
        $this->item_id = $item_id;
        if ($item_id > 0) {
            $iMap['id'] = $item_id;
            $iMap['status'] = 1;
            $iMap['store_id'] = $this->store_id;
            $item = M('Item')->where($iMap)->find();
            if ($item) {
                $this->record_list = D('Stat')->itemRecordList($start_time, $end_time, $item_id, $this->store_id);
            }
        }
        $this->display();
    }


}
